package com.google.gwt.livinglife.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.livinglife.client.ChallengeSelector.SortOrder;
import com.google.gwt.livinglife.client.data.ChallengeData;
import com.google.gwt.livinglife.client.data.LivingLifeGroupData;
import com.google.gwt.livinglife.client.widgets.ChallengeBox;
import com.google.gwt.livinglife.client.widgets.FlashingChallengeWidget;
import com.google.gwt.livinglife.client.widgets.GroupListWidget;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PublicHomePage extends VerticalPanel {
	private DecoratorPanel message_decorator = new DecoratorPanel();
	private VerticalPanel message_container = new VerticalPanel();
	private HorizontalPanel message_link = new HorizontalPanel();
	private HTML faq_link = new HTML("Faq");
	private HTML what_link = new HTML("What can I do here?");
	private HTML founder_message = new HTML("message from LivingLife team");
	private HTML message = new HTML("<h3> We invite you to live your life with passion, excitement and inspiration." +
			"This website is meant for individuals to form groups with people who share the same passions and keep those passions alive by issuing challenges to each other that go beyond routine.");
	private ChallengeData[] top_challenges;
	private DockPanel challenges_groups = new DockPanel();
	private FlashingChallengeWidget challanges_widget = new FlashingChallengeWidget();
	private DecoratorPanel top_group_container = new DecoratorPanel();
	GroupListWidget top_group_widget = new GroupListWidget();
	
	public PublicHomePage() {
		this.add(message_decorator);
		message_decorator.setWidget(message_container);
		message_container.add(message);
		// message_container.add(message_link);
		message_container.setSpacing(5);
		message_link.add(what_link);
		message_link.add(faq_link);
		message_link.add(founder_message);
		this.add(challenges_groups);
		challenges_groups.add(challanges_widget, DockPanel.WEST);
		challenges_groups.add(top_group_container, DockPanel.EAST);
		top_group_container.setWidget(top_group_widget);
		top_group_widget.setHeader("Top Groups");
		LivingLifeServiceAsync service = GWT.create(LivingLifeService.class);
		ChallengeSelector selector = new ChallengeSelector();
		selector.setOrder(SortOrder.TIME);
		selector.setEnd_index(10);
		try {
			service.getChallenges(selector, new AsyncCallback<ChallengeData[]>() {

				public void onFailure(Throwable caught) {
					handleError(caught);
				}

				public void onSuccess(ChallengeData[] result) {
					top_challenges = result;
					challanges_widget.setChallenges(top_challenges);
					challanges_widget.displayRandomChallenge();
				}
			});
		} catch (Exception e) {
			handleError(e);
		}
		try {
			GroupSelector group_selector = new GroupSelector();
			group_selector.setEnd_index(5);
			service.getTopGroups(group_selector, new AsyncCallback<LivingLifeGroupData[]>() {
				public void onFailure(Throwable caught) {
					handleError(caught);
				}

				public void onSuccess(LivingLifeGroupData[] result) {
					for (int i = 0; i < result.length; ++i) {
						top_group_widget.addGroup(result[i]);
					}
				}				
			});
		} catch (Exception e) {
			handleError(e);
		}
	}
	public void onModuleLoad() {
	
	}

	protected void handleError(Throwable caught) {
		Window.alert("Error: " + caught.getMessage());
	}

	public VerticalPanel getMainBody() {
		return this;
	}
}